package jhta;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.sql.Connection;

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;

public class Main extends JFrame {
	
	static int USER_ID = 1;
	static String USER_NAME = "";
	static String fileName = "bookrent.conf";
	String program_name = "중앙 HTA 도서관리 시스템 Ver 1.0 ";
	String localIP;
	
	CodeInput cInput;
	CodeUpdate cUpdate;
	
	RentInput rInput;
	RentUpdate rUpdate;
	
	MemberInput mInput;
	MemberUpdate mUpdate;
	
	BookInput bInput;
	BookUpdate bUpdate;
	
	ReturnInput rtInput;
	ReturnUpdate rtUpdate;
	
	RequestBookInput requestBookInput;
	RequestBookUpdate requestBookUpdate;
	
	
	Connection conn;
	
	Login login;
	
	private JDesktopPane contentPane;
	private JMenuBar menuBar;
	private JMenu mnNewMenu;
	private JMenuItem menuRentInput;
	private JMenuItem menuReturnInput;
	private JMenuItem menuRentUpdate;
	private JMenu mnNewMenu_1;
	private JMenuItem mntmNewMenuItem_3;
	private JMenuItem mntmNewMenuItem_4;
	private JMenuItem menuReturnUpdate;
	private JMenu mnNewMenu_2;
	private JMenu mnNewMenu_3;
	private JMenu mnNewMenu_4;
	private JMenuItem menuMemberInput;
	private JMenuItem menuMemberUpdate;
	private JMenuItem mntmNewMenuItem_8;
	private JMenuItem mntmNewMenuItem_11;
	private JSeparator separator;
	private JMenuItem menuBookInput;
	private JMenuItem menuBookUpdate;
	private JSeparator separator_1;
	private JMenuItem menuRequestInput;
	private JMenuItem menuRequestUpdate;
	
	Main m=this;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main frame = new Main();
					UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
					SwingUtilities.updateComponentTreeUI(frame);
					
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Main() {
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		setJMenuBar(getMenuBar_1());
		contentPane = new JDesktopPane();
		contentPane.setBackground(Color.LIGHT_GRAY);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		// 내 아이피
		try{
			InetAddress ia = InetAddress.getLocalHost();
			localIP = "[" + ia.getHostAddress() + "]";
		}catch(Exception ex){
			
		}
		setTitle2();
		
		
		conn = JavaDBConnection.getConn();
	
		if(conn != null) new CreateTable();
		
		//login();
		
	}
	
	public void setTitle2(){
		setTitle(program_name + localIP + ">>>>" + USER_NAME);
	}
	
	
	
	public void openBookInput(){

		if(bInput == null){
			bInput = new BookInput(this);
			contentPane.add(bInput);
		}
		
		bInput.setVisible(true);
		bInput.toFront();
		
	}
	
	public void openBookUpdate(){

		if(bUpdate == null){
			bUpdate = new BookUpdate();
			contentPane.add(bUpdate);
		}
		
		bUpdate.setVisible(true);
		bUpdate.toFront();
	}
		
	
	public void login(){
		if(login != null){
			contentPane.remove(login);
			contentPane.updateUI();
			login.setVisible(false);
			login.dispose();
			System.out.println("login dispose");
		}
		login = new Login(Main.this);
		login.setModal(true);
		login.setVisible(true);
	}
	
	private JMenuBar getMenuBar_1() {
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.add(getMnNewMenu());
			menuBar.add(getMnNewMenu_2());
			menuBar.add(getMnNewMenu_3());
			menuBar.add(getMnNewMenu_4());
			menuBar.add(getMnNewMenu_1());
		}
		return menuBar;
	}
	private JMenu getMnNewMenu() {
		if (mnNewMenu == null) {
			mnNewMenu = new JMenu("대여관리");
			mnNewMenu.add(getMenuRentInput());
			mnNewMenu.add(getMenuRentUpdate());
			mnNewMenu.add(getSeparator());
			mnNewMenu.add(getMenuReturnInput());
			mnNewMenu.add(getMenuReturnUpdate());
		}
		return mnNewMenu;
	}
	private JMenuItem getMenuRentInput() {
		if (menuRentInput == null) {
			menuRentInput = new JMenuItem("대여입력");
			menuRentInput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					if(USER_ID == 0){
						login();
						return;
					}
					
					if(rInput == null){
						Main mm=new Main();
						rInput = new RentInput(Main.this);
						contentPane.add(rInput);
					}
					rInput.setVisible(true);
					rInput.toFront();
					
				}
			});
		}
		return menuRentInput;
	}
	private JMenuItem getMenuReturnInput() {
		if (menuReturnInput == null) {
			menuReturnInput = new JMenuItem("반납입력");
			menuReturnInput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(USER_ID == 0){
						login();
						return;
					}

					if(rtInput == null){
						rtInput = new ReturnInput();
						contentPane.add(rtInput);
						
					}
					rtInput.setVisible(true);
					rtInput.toFront();
				}
			});
		}
		return menuReturnInput;
	}
	private JMenuItem getMenuRentUpdate() {
		if (menuRentUpdate == null) {
			menuRentUpdate = new JMenuItem("대여정보 조회 및 수정");
			menuRentUpdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(USER_ID == 0){
						login();
						return;
					}

					if(rUpdate == null){
						
						rUpdate = new RentUpdate(Main.this);
						contentPane.add(rUpdate);
					}
					rUpdate.setVisible(true);
					rUpdate.toFront();

					
				}
			});
		}
		return menuRentUpdate;
	}
	private JMenu getMnNewMenu_1() {
		if (mnNewMenu_1 == null) {
			mnNewMenu_1 = new JMenu("접속");
			mnNewMenu_1.add(getMntmNewMenuItem_3());
			mnNewMenu_1.add(getMntmNewMenuItem_4());
			
			JMenuItem mntmNewMenuItem = new JMenuItem("\uB370\uC774\uD130\uBCA0\uC774\uC2A4 \uC11C\uBC84\uC8FC\uC18C");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					String temp = JOptionPane.showInputDialog(null, "접속할 데이터베이스 서버 주소를 입력하세요", JavaDBConnection.serverIP);
					if(!temp.equals("")){
						conn = JavaDBConnection.getConn(temp);
					}
				}
			});
			mnNewMenu_1.add(mntmNewMenuItem);
		}
		return mnNewMenu_1;
	}
	private JMenuItem getMntmNewMenuItem_3() {
		if (mntmNewMenuItem_3 == null) {
			mntmNewMenuItem_3 = new JMenuItem("로그인");
			mntmNewMenuItem_3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					login();
		
				}
			});
		}
		return mntmNewMenuItem_3;
	}
	private JMenuItem getMntmNewMenuItem_4() {
		if (mntmNewMenuItem_4 == null) {
			mntmNewMenuItem_4 = new JMenuItem("로그아웃");
			mntmNewMenuItem_4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					USER_ID = 0;
					USER_NAME = "";
					JOptionPane.showMessageDialog(null, "로그아웃되었습니다....감사합니다!!!");
					setTitle2();
					
				}
			});
		}
		return mntmNewMenuItem_4;
	}
	private JMenuItem getMenuReturnUpdate() {
		if (menuReturnUpdate == null) {
			menuReturnUpdate = new JMenuItem("반납정보 수정");
			menuReturnUpdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(USER_ID == 0){
						login();
						return;
					}

					if(rtUpdate == null){
						rtUpdate = new ReturnUpdate();
						contentPane.add(rtUpdate);
					}
					rtUpdate.setVisible(true);
					rtUpdate.toFront();
				}
			});
		}
		return menuReturnUpdate;
	}
	private JMenu getMnNewMenu_2() {
		if (mnNewMenu_2 == null) {
			mnNewMenu_2 = new JMenu("회원관리");
			mnNewMenu_2.add(getMenuMemberInput());
			mnNewMenu_2.add(getMenuMemberUpdate());
		}
		return mnNewMenu_2;
	}
	private JMenu getMnNewMenu_3() {
		if (mnNewMenu_3 == null) {
			mnNewMenu_3 = new JMenu("도서관리");
			mnNewMenu_3.add(getMenuBookInput());
			mnNewMenu_3.add(getMenuBookUpdate());
			mnNewMenu_3.add(getSeparator_1());
			mnNewMenu_3.add(getMenuRequestInput());
			mnNewMenu_3.add(getMenuRequestUpdate());
		}
		return mnNewMenu_3;
	}
	private JMenu getMnNewMenu_4() {
		if (mnNewMenu_4 == null) {
			mnNewMenu_4 = new JMenu("코드관리");
			mnNewMenu_4.add(getMntmNewMenuItem_8());
			mnNewMenu_4.add(getMntmNewMenuItem_11());
		}
		return mnNewMenu_4;
	}
	private JMenuItem getMenuMemberInput() {
		if (menuMemberInput == null) {
			menuMemberInput = new JMenuItem("회원정보 입력");
			menuMemberInput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(USER_ID == 0){
						login();
						return;
					}

					if(mInput == null){
						mInput = new MemberInput(Main.this);
						contentPane.add(mInput);
					}
					mInput.setVisible(true);
					mInput.toFront();

				}
			});
		}
		return menuMemberInput;
	}
	private JMenuItem getMenuMemberUpdate() {
		if (menuMemberUpdate == null) {
			menuMemberUpdate = new JMenuItem("회원정보 조회 및 수정");
			menuMemberUpdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(USER_ID == 0){
						login();
						return;
					}

					if(mUpdate == null){
						mUpdate = new MemberUpdate();
						contentPane.add(mUpdate);
					}
					mUpdate.setVisible(true);
					mUpdate.toFront();

				}
			});
		}
		return menuMemberUpdate;
	}
	private JMenuItem getMntmNewMenuItem_8() {
		if (mntmNewMenuItem_8 == null) {
			mntmNewMenuItem_8 = new JMenuItem("분류 코드입력");
			mntmNewMenuItem_8.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(USER_ID == 0){
						login();
						return;
					}

					if(cInput == null){
						cInput = new CodeInput(1);
						contentPane.add(cInput);
					}
					cInput.setVisible(true);
					cInput.toFront();
				}
			});
		}
		return mntmNewMenuItem_8;
	}
	private JMenuItem getMntmNewMenuItem_11() {
		if (mntmNewMenuItem_11 == null) {
			mntmNewMenuItem_11 = new JMenuItem("코드조회 및 수정");
			mntmNewMenuItem_11.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(USER_ID == 0){
						login();
						return;
					}

					if(cUpdate == null){
						cUpdate = new CodeUpdate();
						contentPane.add(cUpdate);
					}
					
					cUpdate.setVisible(true);
					cUpdate.toFront();
				}
			});
		}
		return mntmNewMenuItem_11;
	}
	private JSeparator getSeparator() {
		if (separator == null) {
			separator = new JSeparator();
		}
		return separator;
	}
	private JMenuItem getMenuBookInput() {
		if (menuBookInput == null) {
			menuBookInput = new JMenuItem("도서입력");
			menuBookInput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(USER_ID == 0){
						login();
						return;
					}

					openBookInput();
				}
			});
		}
		return menuBookInput;
	}
	private JMenuItem getMenuBookUpdate() {
		if (menuBookUpdate == null) {
			menuBookUpdate = new JMenuItem("도서 정보 조회 및 수정");
			menuBookUpdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(USER_ID == 0){
						login();
						return;
					}

					openBookUpdate();
				}
			});
		}
		return menuBookUpdate;
	}
	private JSeparator getSeparator_1() {
		if (separator_1 == null) {
			separator_1 = new JSeparator();
		}
		return separator_1;
	}
	private JMenuItem getMenuRequestInput() {
		if (menuRequestInput == null) {
			menuRequestInput = new JMenuItem("도서 요청");
			menuRequestInput.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(USER_ID == 0){
						login();
						return;
					}

					if(requestBookInput == null){
						requestBookInput = new RequestBookInput();
						contentPane.add(requestBookInput);
					}
					
					requestBookInput.setVisible(true);
					requestBookInput.toFront();
				}
			});
		}
		return menuRequestInput;
	}
	private JMenuItem getMenuRequestUpdate() {
		if (menuRequestUpdate == null) {
			menuRequestUpdate = new JMenuItem("도서 요청조회 및 수정");
			menuRequestUpdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					if(USER_ID == 0){
						login();
						return;
					}

					if(requestBookUpdate == null){
						requestBookUpdate = new RequestBookUpdate();
						contentPane.add(requestBookUpdate);
					}
					
					requestBookUpdate.setVisible(true);
					requestBookUpdate.toFront();
				}
			});
		}
		return menuRequestUpdate;
	}
}
